﻿
using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web.Mvc;
//using www.SService;

namespace www.Controllers
{
    public class RelaxController:Controller
    {
        //private readonly ServiceClient _sService1 = new ServiceClient();
        //[HttpGet]
        //public ActionResult Home(string url)
        //{
        //    ViewBag.ReturnUrl = url;
            
        //    return View();
        //}

        //[HttpPost]
        //public ActionResult Home(FormCollection collection)
        //{
            
        //    string user = collection["username"];
        //    var pass = collection["password"];
        //    var first = _sService1.GetAllUser().FirstOrDefault(p => p.UserName == user);
        //    if (first != null)
        //    {
        //        if (pass == Decrypt("Dragon_1402", first.Password))
        //        {
        //            Session["ok"] = true;
        //            Session["username"] = user;
        //        }
        //        else
        //        {
        //            ViewBag.Message = "bạn nhập sai password.";
        //            Session["ok"] = false;
        //        }
        //    }
        //    else
        //    {
        //        ViewBag.Message = "Tên đăng nhập không tồn tại.";
        //        Session["ok"] = false;
        //    }
           
        //    if (Convert.ToBoolean(Session["ok"]))
        //         return RedirectToAction("Index", "Home");
        //    Response.Write("<script>alert('" + ViewBag.Message + "');</script>");
        //    return RedirectToAction("Home", "Relax");
        //}
        //public ActionResult Logoff(FormCollection collection)
        //{
            
        //    collection["username"]="";
        //    collection["password"]="";
            
        //        Session["ok"] = false;

        //      return  RedirectToAction("Index", "Home");
        //}
        ///// <summary>
        ///// Mã hóa ký tự với kiểu mã hõa TripleDes - MD5 
        ///// </summary>
        ///// <param name="key"></param>
        ///// <param name="toEncrypt"></param>
        ///// <returns></returns>
        //public string Encrypt(string key, string toEncrypt)
        //{
           
        //    var toEncryptArray = Encoding.UTF8.GetBytes(toEncrypt);
        //    var hashmd5 = new MD5CryptoServiceProvider();
        //    var keyArray = hashmd5.ComputeHash(Encoding.UTF8.GetBytes(key));
        //    var tdes =new TripleDESCryptoServiceProvider
        //    {
        //        Key = keyArray,
        //        Mode = CipherMode.ECB,
        //        Padding = PaddingMode.PKCS7
        //    };
        //    var cTransform = tdes.CreateEncryptor();
        //    var resultArray = cTransform.TransformFinalBlock(
        //          toEncryptArray, 0, toEncryptArray.Length);
        //    return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        //}
        ///// <summary>
        ///// Giải mã dữ liệu đã mã hóa 
        ///// </summary>
        ///// <param name="key"></param>
        ///// <param name="toDecrypt"></param>
        ///// <returns></returns>
        //public string Decrypt(string key, string toDecrypt)
        //{
        //    var toEncryptArray = Convert.FromBase64String(toDecrypt);
        //    var hashmd5 = new MD5CryptoServiceProvider();
        //    var keyArray = hashmd5.ComputeHash(Encoding.UTF8.GetBytes(key));
        //    var tdes = new TripleDESCryptoServiceProvider
        //    {
        //        Key = keyArray,
        //        Mode = CipherMode.ECB,
        //        Padding = PaddingMode.PKCS7
        //    };
        //    var cTransform = tdes.CreateDecryptor();
        //    var resultArray = cTransform.TransformFinalBlock(
        //     toEncryptArray, 0, toEncryptArray.Length);
        //    return Encoding.UTF8.GetString(resultArray);
        //}
    }
    
}